{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ac24f0c",
   "metadata": {},
   "source": [
    "# 可变与不可变类型\n",
    "\n",
    "## 列表是可变的\n",
    "\n",
    "列表是一种可变类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f7d078b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba3c2fcd",
   "metadata": {},
   "source": [
    "可以通过索引改变："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "69ee7b7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[0] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "454f2ec9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 2, 3, 4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9375ceeb",
   "metadata": {},
   "source": [
    "通过方法改变："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5f93fdee",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.insert(3, 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1042b457",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 2, 3, 200, 4]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ceb7769",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c9e7efdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 100, 200]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a5ad78d",
   "metadata": {},
   "source": [
    "## 字符串是不可变的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d01d0d60",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"hello world\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86cf8c14",
   "metadata": {},
   "source": [
    "通过索引改变会报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "79241a3a",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mz\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "s[0] = 'z'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e95dc6ab",
   "metadata": {},
   "source": [
    "字符串方法只是返回一个新字符串，并不改变原来的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "642e4d9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello Mars'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.replace('world', 'Mars')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d0424bd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe9533c7",
   "metadata": {},
   "source": [
    "想改变某个字符串变量的值，只能重新赋值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "69ec019e",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = s.replace('world', 'Mars')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ba94556b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello Mars'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a9f8c5c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
